import BaseCacheData from "~/store/base/baseCacheData";
import system_role from "~/apis/api/system_role";

class RoleCache extends BaseCacheData {
  roles: Array<Department> = [];
  roleSelectSource: Array<ForeignKeyModel> = [];
  roleTreeSelectSource: Array<TreeForeignKeyModel> = [];
  noSelectNode: any = { value: "0", text: "不设置" };

  constructor() {
    super();
    this.update();
  }

  update() {
    super.update();
    system_role
      .getAll()
      .then((res) => {
        this.roles = res.data;
        this.roleSelectSource = super.toForeignKeyDataList(
          res.data,
          "name",
          "id"
        );
        this.roleTreeSelectSource = super.toTree(
          res.data,
          "id",
          "parentId",
          "name"
        );
        this.roleTreeSelectSource = [
          this.noSelectNode,
          ...this.roleTreeSelectSource,
        ];
        this.roleSelectSource = [this.noSelectNode, ...this.roleSelectSource];
        return this;
      })
      .catch((err) => {
        return null;
      });
  }
}

export default new RoleCache();
